/*
 * Copyright 2002-2006 the original author or authors. Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and limitations under the
 * License.
 */
package pikes.html;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.xml.XMLConstants;

import pikes.xml.XMLDocumentView;
import pikes.xml.XMLTag;

/**
 * Abstract view which renders a HTML web page to the response's writer. Subclasses should provide a root html tag and
 * doc type. The user of this class can set the following properties: <code>
 * <ul>
 *  <li><code>shouldPrintHtmlNamespace</code>. If true, html namespace (&quot;http://www.w3.org/1999/xhtml&quot;) will be added to the root html tag as attribute (the default is true)</li>
 * </ul>
 * </code>
 * @author Peter Bona
 * @since 0.0.1
 */
public abstract class AbstractHtmlPage extends XMLDocumentView {
	/**
	 * If true, html namespace (&quot;http://www.w3.org/1999/xhtml&quot;) will be added to the root html tag as
	 * attribute (the default is true).
	 */
	private boolean shouldPrintHtmlNamespace = true;

	@Override
	final protected XMLTag createRootTag(Map model, HttpServletRequest request) throws Exception {
		XMLTag htmlTag = createRootHtmlTag(model, request);

		if (shouldPrintHtmlNamespace) {
			htmlTag.addAttribute(XMLConstants.XMLNS_ATTRIBUTE, "http://www.w3.org/1999/xhtml");
		}

		return htmlTag;
	}

	/**
	 * Creates the root html tag for the web page.
	 * @param model
	 * @param request
	 * @return the root html tag
	 * @throws Exception
	 */
	protected abstract XMLTag createRootHtmlTag(Map model, HttpServletRequest request) throws Exception;

	/**
	 * Provides Html document type declaration.
	 * @return the HTML doc type
	 */
	// protected abstract HtmlDocType getDocType();
	/**
	 * @return true, if the html namespace should be added as attribute, false otherwise.
	 */
	public boolean shouldPrintHtmlNamespace() {
		return shouldPrintHtmlNamespace;
	}

	/**
	 * @param shouldPrintHtmlNamespace if true, the html namespace will be added as attribute.
	 */
	public void shouldPrintHtmlNamespace(boolean shouldPrintHtmlNamespace) {
		this.shouldPrintHtmlNamespace = shouldPrintHtmlNamespace;
	}
}
